﻿Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Text.RegularExpressions
Imports System.Collections
Imports System.Threading

Public Class Partager
    Public Shared cltRech As Boolean = True
    Public Shared advRech As Boolean = True
    Public Shared has As Hashtable
    Public Shared forPlainteRech As Boolean
    Public Shared thrd2 As Thread
    Public Shared isRendezVousAdd As Boolean = True ' est ce qu'on vient d'ajouter un rendez-vous, pour que le timer être exécuter
    Public Shared dv As Dossier
    Public Shared idDossier As Integer = 0
    Public Shared dossAvoEne As Dossier_Avocat_Adversaire
    Public Shared dossAvo As dossAvoBur
    Public Shared ligne As DataRow
    Public Shared sr As StreamReader
    Public Shared sw As StreamWriter
    Public Shared ref As Integer
    Public Shared flag As Boolean
    Public Shared archiv As Boolean = False
    Public Shared detail As Boolean = False
    Public Shared refObj As Reference
    Public Shared serveur As String = "."
    Public Shared con As SqlConnection
    Public Shared conPhone As SqlConnection
    Public Shared frmMoule As Form
    Public Shared frmafect As Form
    Public Shared viensDeProc As Boolean 'Pour remplir combo Tribunal du Form Proce, Aussi pour savoir si j'ai ouvré le form Envois depuis proce
    Public Shared procSelected As Proce
    Public Shared doubleVider As Boolean = False
    Public Shared pltObje As Plainte
    Public Shared nonInitial As ArrayList 'Les contrôles à ne pas initialiser
    Public Shared dr As SqlDataReader
    Public Shared ds As DataSet
    Public Shared ad As SqlDataAdapter
    Public Shared moi As New Compte
    Public Shared list_recherch As New ArrayList
    Public Shared list_hatif As New ArrayList  'list pour portables
    Public Shared list_fix As New ArrayList
    Public Shared list_fax As New ArrayList
    Public Shared list_piece As New ArrayList 'piece jointe
    Public Shared type As String
    Public Shared ar As Boolean = False
    Public Shared hadf As String
    Public Shared typePhone As String
    Public Shared cmd As SqlCommand
    Public Shared wadifa As String
    Public Shared isFixClicked As Boolean = False
    Public Shared isTelClicked As Boolean = False
    Public Shared isFaxClicked As Boolean = False
    Public Shared type_rech_mowakil As String
    Public Shared type_rech_adver As String
    Public Shared checkHand As Boolean
    Public Shared deClt As DictionaryEntry
    Public Shared deAdv As DictionaryEntry
    Public Shared deMarji As DictionaryEntry
    Public Shared deOrigin As DictionaryEntry
    Public Shared rech As Boolean  ' est ce que je suis vevnu du form Idafa_Mowakil
    Public Shared idAvoDoss As Integer
    Public Shared nomMohami As String
    Public Shared user As Compte
    Public Shared line As ListViewItem
    Public Shared list_crystal As New ArrayList
    Public Shared cmpform As CompteFrom
    Public Shared utilist As Compte
    Public Shared type_rech_Avo_adver As String
    Public Shared Administrat As Administrateur
    Public Shared lstDecTarde As ArrayList
    Public Shared lstChemin() As String 'Contient les chemins client des piece jointes
    Public Shared tableByte As Byte()
    Public Shared IdsDossiers() As Integer
    Public Shared DateEntrer As DateTime
    Public Shared typee As String
    Public Shared tabColumns() As String
    ''' <summary>
    ''' Cette procédure remplie un dataset avec les sessions d'assises retardés 
    ''' </summary>
    ''' <remarks>remark</remarks>
    Public Shared Sub remplirRapportControle()
        Dim comp As Integer = 0
        Partager.ds = New DataSet1
        Partager.ds.Tables("Jalassat").Rows.Clear()

        Dim cmd As New SqlClient.SqlCommand("select t.*,Mo9arir.Nom As nomDecid from " & _
"(select d.Tribunal,i.*,d.Num_Dossier,c.Nom + ' ' + c.Prenom as NomComplet, c.Nom_entreprise " & _
            ",a.Nom + ' ' + a.Prenom as NomCompletAd, a.Nom_entreprise as nomEntrAdv " & _
            "from refAdv,refClt,Dossier d,Ijraa i,Clients c,Adversaire a " & _
"where  c.id = refClt.idClt " & _
"and a.id = refAdv.idAdv " & _
"and d.Ref = refClt.idRef " & _
"and refClt.idRef = refAdv.idRef " & _
"and i.Id_Dossier = d.Id " & _
"and d.Archive = 0 " & _
"and d.Id not in ( " & _
"select Id_Dossier from Ijraa where Date_Ijraa >= replace(convert(varchar, getdate(), 111), '/', '-')) " & _
"and " & _
"(d.Id not in (select Id_Dossier from Decision where [Type] = 'قطعي') " & _
 "or d.Id not in (select Id_Dossier from Decision)))t left join Mo9arir " & _
 "on Mo9arir.Id_Dossier = t.Id_Dossier" & _
 "  order by t.Id_Dossier,Date_Ijraa desc", Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        If Partager.dr.HasRows Then
            ligne = ds.Tables("Jalassat").NewRow()
            ligne("Num_Dossier") = -1
            While Partager.dr.Read
                If dr("Num_Dossier") <> ligne("Num_Dossier") Then
                    ligne = ds.Tables("Jalassat").NewRow()
                    ReDim Preserve IdsDossiers(comp)
                    IdsDossiers(comp) = dr.GetInt32(2)
                    ligne("Date") = Format(dr("Date_ijraa"), "dd/MM/yyyy")
                    ligne("jour") = dr("Jour")
                    ligne("Salle") = dr("Salle")
                    ligne("Cause") = dr("Cause")
                    ligne("Ijra2") = dr("Ijraa")
                    ligne("Heur") = dr("Heur")
                    ligne("Tribunal") = dr("Tribunal")
                    ligne("Num_Dossier") = dr("Num_Dossier")
                    If dr("nomDecid") Is DBNull.Value Then
                        ligne("Mo9arir") = ""
                    Else
                        ligne("Mo9arir") = dr("nomDecid")
                    End If
                    ligne("Client") = CStr(dr("NomComplet")) & CStr(dr("Nom_entreprise"))
                    ligne("Adversaire") = CStr(dr("NomCompletAd")) & CStr(dr("nomEntrAdv"))
                    Partager.ds.Tables("Jalassat").Rows.Add(ligne)
                    comp += 1
                End If
            End While
        End If
        Partager.close()

    End Sub

    Public Shared Sub selectYear(ByRef cmb As ComboBox)
        Dim myDate As Date = Date.Now
        Dim myYear As Integer = myDate.Year
        cmb.SelectedItem = myYear
    End Sub

    Public Shared Sub remplirRendezVous()
        moi.lstRendezVousStruct = New List(Of Partager.rendezVousStruct)
        Dim cmd As New SqlClient.SqlCommand("select * from Rendez_Vous where CONVERT(VARCHAR(10), [date], 103)  = CONVERT(VARCHAR(10), GETDATE(), 103) ", Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader

        Dim i As Integer = 0
        Dim nbrchamp As Integer = dr.FieldCount
        Dim khab As Rendez_vous

        While dr.Read
            moi.listverif = New ArrayList
            For i = 0 To nbrchamp - 1
                If Partager.dr(i) Is DBNull.Value Then
                    moi.listverif.Add(Nothing)
                Else
                    moi.listverif.Add(Partager.dr(i))
                End If
            Next

            rendVsStruct = New Partager.rendezVousStruct
            rendVsStruct.rendezVous = New Rendez_vous(moi.listverif)
            Dim heurRendeV As New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, CInt(rendVsStruct.rendezVous.Heure.Split(":")(0)), CInt(rendVsStruct.rendezVous.Heure.Split(":")(1)), 0)
            rendVsStruct.dateAlarme = heurRendeV
            moi.lstRendezVousStruct.Add(rendVsStruct)
        End While
        moi.lstRendezVousStruct = moi.lstRendezVousStruct.OrderBy(Function(x) x.dateAlarme).ToList()
        Partager.close()
    End Sub

    Public Shared Sub conne() ' test
        If File.Exists("ConnectionString.txt") = True Then
            Partager.sr = New StreamReader("ConnectionString.txt")
            Do Until Partager.sr.Peek = -1
                serveur = Partager.sr.ReadLine()
            Loop
            Partager.sr.Close()
        End If

    End Sub

    'Il remplit les combos ville
    Public Shared Sub com_ville(ByRef combo As ComboBox, ByVal nomTable As String, ByVal nomChamp As String)
        Try
            With combo
                .Text = ""
                .Items.Clear()
                cmd = New SqlCommand("Select * From " & nomTable, con)
                open()
                dr = cmd.ExecuteReader()
                Do While dr.Read()
                    .Items.Add(dr(nomChamp))
                Loop
                dr.Close()
                close()
                If .Items.Count > 0 Then
                    .SelectedIndex = 0
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            close()
        End Try
    End Sub

    Public Shared Sub rempliCombClient(ByRef combo As ComboBox, ByVal requete As String, ByVal nomChamp As String)
        Try
            With combo
                .Items.Clear()
                cmd = New SqlCommand(requete, con)
                open()
                dr = cmd.ExecuteReader()
                Do While dr.Read()
                    .Items.Add(dr(nomChamp))
                Loop
                dr.Close()
                close()
                If .Items.Count > 0 Then
                    .SelectedIndex = 0
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            close()
        End Try
    End Sub

    Public Shared Sub StandardInitial() 'Initialise tous les objets dont on a utilisé
        Dim cou As Integer = Application.OpenForms.Count - 1
        Dim i As Integer = 0
        While i <= cou
            If Application.OpenForms.Item(i).Name <> "Frm_Main" And Application.OpenForms.Item(i).Name <> "Frm_Login" Then
                Application.OpenForms.Item(i).Close()

                cou -= 1
                i -= 1
            End If
            i += 1
        End While
        list_hatif.Clear()
        list_fix.Clear()
        list_fax.Clear()
    End Sub
    Public Shared Sub deselectViewRows(ByVal lv As ListView)
        For Each it As ListViewItem In lv.Items
            If it.Selected Then
                it.Selected = False
            End If
        Next
    End Sub
    Public Shared Sub ControleNull(ByVal frm As Control)
        For Each txt As Control In frm.Controls
            If TypeOf txt Is TextBox Then
                If txt.Text = "" Then
                    txt.Text = Nothing
                End If
            End If
        Next
    End Sub

    Public Shared Function regEmail(ByVal Email As String) As Boolean
        Dim myRegex As System.Text.RegularExpressions.Regex
        myRegex = New Regex("^([+-.\w]+)@([-.\w]+)\.([-.\w]+)$")
        Dim l As Boolean = myRegex.IsMatch(Email)
        Return l
    End Function

    Public Shared Sub vider(ByVal fr As Form, Optional ByVal tnom As ArrayList = Nothing)
        Dim classment As Integer = 0
        Dim flag As Boolean
        For Each txt As Control In fr.Controls


            flag = True
            If Not tnom Is Nothing Then
                For Each nom As String In tnom
                    If txt.Name = nom Then
                        flag = False
                        Exit For
                    End If
                Next
            End If


            If flag Then
                If TypeOf txt Is TextBoxBase Then
                    If CType(txt, TextBoxBase).Enabled Then
                        CType(txt, TextBoxBase).Clear()
                    End If
                ElseIf TypeOf txt Is PictureBox Then
                    CType(txt, PictureBox).Image = My.Resources.photo
                ElseIf TypeOf txt Is ComboBox Then
                    If txt.Name <> "Comb_marji3ona" And txt.Name <> "Comb_kholassa" Then
                        If CType(txt, ComboBox).Items.Count > 0 Then
                            CType(txt, ComboBox).SelectedIndex = 0
                        End If
                    End If
                ElseIf TypeOf txt Is ListView Then
                    If CType(txt, ListView).SelectedIndices.Count > 0 Then
                        For i As Integer = 0 To CType(txt, ListView).SelectedIndices.Count - 1
                            CType(txt, ListView).Items(CType(txt, ListView).SelectedIndices(i)).Selected = False
                            doubleVider = True
                        Next
                    End If
                ElseIf TypeOf txt Is RadioButton Then
                    classment += 1
                    If classment = 1 Then
                        CType(txt, RadioButton).Checked = True
                    End If
                ElseIf TypeOf txt Is MaskedTextBox Then
                    CType(txt, MaskedTextBox).Clear()
                ElseIf TypeOf txt Is DateTimePicker Then
                    CType(txt, DateTimePicker).Value = Now
                End If
                ' txt.Text = ""
                'ElseIf TypeOf txt Is GroupBox Or TypeOf txt Is Panel Then
                '    vider(txt)
                'End If

            End If
        Next
        list_fix.Clear()
        list_fax.Clear()
        list_hatif.Clear()
        doubleVider = False
    End Sub
    '----------Les tabless Combo
    Public Shared Sub RemplirList(ByRef list As ListView, ByVal nomTable As String, ByVal nomChamp As String)
        Try
            list.Items.Clear()
            Dim Cmd As New SqlCommand("Select * From " & nomTable, Partager.con)
            Partager.open()
            Partager.dr = Cmd.ExecuteReader()
            Do While Partager.dr.Read()
                Dim Line As ListViewItem = New ListViewItem(New String() {Partager.dr(nomChamp)})
                list.Items.Add(Line)
            Loop
            Partager.dr.Close()
            Partager.close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Partager.close()
        End Try
    End Sub
    '---------remplir list mo9arir
    Public Shared Sub Remplir(ByRef list As List(Of String), ByVal nomTable As String, ByVal nomChamp As String)
        Try
            list.Clear()
            Dim Cmd As New SqlCommand("Select * From " & nomTable, Partager.con)
            Partager.open()
            Partager.dr = Cmd.ExecuteReader()
            Do While Partager.dr.Read()
                list.Add(Partager.dr(nomChamp))
            Loop
            Partager.dr.Close()
            Partager.close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Partager.close()
        End Try
    End Sub

    Public Shared Sub DeleteFromList(ByRef list As ListView, ByVal nomTable As String, ByVal nomChamp As String, ByVal txt As TextBox)
        Dim vil As String = ""
        With list
            If .SelectedIndices.Count > 0 Then
                For i As Integer = 0 To .SelectedIndices.Count - 1
                    vil &= .Items(.SelectedIndices(i)).SubItems(0).Text
                Next
                Try
                    Partager.cmd = New SqlCommand("Delete From " & nomTable & " Where " & nomChamp & " = @val", Partager.con)
                    cmd.Parameters.Add("@val", SqlDbType.VarChar).Value = vil
                    Partager.open()
                    Partager.cmd.ExecuteNonQuery()
                    Partager.close()

                    Partager.RemplirList(list, nomTable, nomChamp)
                    txt.Clear()
                    txt.Select()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                    Partager.close()
                End Try
            End If
        End With
    End Sub

    Public Shared Sub AjouterWadifa(ByRef txt As TextBox, ByRef list As ListView, ByRef nomTable As String, ByVal nomChamp As String)
        If (txt.Text <> "") Then
            Try
                cmd = New SqlCommand("insert into " & nomTable & " (" & nomChamp & ") values (@wadifa)", Partager.con)
                cmd.Parameters.Add("@wadifa", SqlDbType.VarChar).Value = txt.Text
                open()
                cmd.ExecuteNonQuery()
                close()
                RemplirList(list, nomTable, nomChamp)
                txt.Clear()
                txt.Select()
            Catch sqlExp As SqlException
                If sqlExp.Message.Contains("uniqueTribunal") Then
                    MsgBox("هذه المحكمة توجد من قبل", MsgBoxStyle.Critical, "خطأ")
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                Partager.close()
            End Try
        End If
    End Sub
    '----End Table Combo

    Public Shared Function GetPhoto(ByVal filePath As String) As Byte()
        Dim photo() As Byte
        If filePath = "" Then
            photo = Nothing
        Else

            Dim stream As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
            Dim reader As BinaryReader = New BinaryReader(stream)
            photo = reader.ReadBytes(stream.Length)
            reader.Close()
            stream.Close()

        End If
        Return photo
    End Function

    Public Shared Sub close()

        Try
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Shared Sub open()
        Try
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


    Public Shared Function image(ByVal Pic_photo As PictureBox) As Byte()
        Dim ms As MemoryStream = New MemoryStream()
        Pic_photo.Image.Save(ms, ImageFormat.Jpeg)
        Dim bytBLOBData(ms.Length - 1) As Byte
        ms.Position = 0
        ms.Read(bytBLOBData, 0, ms.Length)
        Return bytBLOBData
    End Function

    'مرجعنا
    Public Shared Sub setfMarji3(ByRef cmb As ComboBox)
        open()
        cmd = New SqlCommand("select top 1 Ref from Reference order by Ref desc", con)
        Dim marji3 As Integer = cmd.ExecuteScalar
        If marji3 <> 0 Then
            cmb.Text = CType(marji3 + 1, String)
        Else
            cmb.Text = 1
        End If
    End Sub

    Public Shared Function getDayName(ByVal d As Date) As String
        Dim name As String = ""
        Select Case d.DayOfWeek
            Case 0
                name = "الأحد"
            Case 1
                name = "الإثنين"
            Case 2
                name = "الثلاثاء"
            Case 3
                name = "الأربعاء"
            Case 4
                name = "الخميس"
            Case 5
                name = "الجمعة"
            Case 6
                name = "السبت"
        End Select
        Return name
    End Function
    Public Shared Function reverseString(ByRef str As String) As String
        Dim str2 As String
        str2 = str.Split("/")(2)
        str2 += "/" & str.Split("/")(1)
        str2 += "/" & str.Split("/")(0)
        Return str2
    End Function
    Public Shared lstForm As List(Of Formulaire)
    Public Shared Sub remplirListForm(ByRef lst As List(Of Formulaire))
        Dim cmd As SqlCommand = New SqlCommand("select * from Form", con)
        open()
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            lst = New List(Of Formulaire)
            While dr.Read
                lst.Add(New Formulaire With {.Id = dr.GetInt32(0), .legende = dr.GetString(2), .Nom = dr.GetString(1)})
            End While
        End If
        dr.Close()
        close()
    End Sub

    Public Shared Sub remplirLstView(ByRef lst As ListView)
        Dim cmd As SqlCommand = New SqlCommand("select * from Form", con)
        open()
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                line = New ListViewItem(New String() {dr(0), dr(2)})
                lst.Items.Add(line)
            End While
        End If
        close()
    End Sub

    Public Shared Sub verifPriv(ByRef frm As Form)
        'voir si cet utilisateur a un privilège
        If moi.type = False Then
            Dim arra As CompteFrom = Nothing
            If Not moi.listForm Is Nothing Then
                remplirListForm(lstForm)
                For Each arL In moi.listForm
                    Dim req As IEnumerable(Of String) = From frmReq In Partager.lstForm
                                                        Where frmReq.Id = arL.Id_Form
                                                        Select frmReq.legende

                    If frm.Text = req(0) Then
                        arra = New CompteFrom
                        arra = arL
                        Exit For
                    End If
                Next
            End If

            If Not IsNothing(arra) Then
                If arra.isRead = True Then
                    For Each btn As Control In frm.Controls
                        If TypeOf btn Is Button Then
                            If Not CType(btn, Button).Name.Contains("rech") And Not CType(btn, Button).Name.Contains("tele") _
                                And Not CType(btn, Button).Name.Contains("fax") And Not CType(btn, Button).Name.Contains("morfa9") Then
                                btn.Enabled = False
                            End If
                        End If
                    Next
                End If
                frm.MdiParent = Frm_Main
                frm.Show()
            Else
                frm.Close()
                MsgBox("ليست لديك الصلاحية للدخول", MsgBoxStyle.Critical, "خطأ")
            End If
        Else
            frm.MdiParent = Frm_Main
            frm.Show()
        End If
    End Sub


    Public Sub remplirmLogins(ByVal cmb As ComboBox)
        'Dim cmd As SqlCommand = New SqlCommand("select id,Nom_Complet from Compte", con)
        'open()
        'dr = cmd.ExecuteReader
        'If dr.HasRows Then
        '    While dr.Read
        '        line = New ListViewItem(New String() {dr(0), dr(1)})
        '        lst.Items.Add(line)
        '    End While
        'End If
        'close()
    End Sub


    'Remplit le comboBox par le nom et le prenom avec un id
    Public Shared Sub remplirClient(ByRef cmb As ComboBox)
        moi.remplir_List_client()
        cmb.Items.Clear()
        cmb.Text = ""
        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each clt As Client In Partager.moi.List_client
            If clt.Type = "ذاتي" Then
                str = clt.Nom & "  " & clt.Prenom
            ElseIf clt.Type = "معنوي" Then
                str = clt.Nom_ent
            End If
            htClient.Add(clt.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            cmb.Items.Add(ImageType)
        Next
        cmb.DisplayMember = "value"
        cmb.ValueMember = "key"
        If cmb.Items.Count > 0 Then
            cmb.SelectedIndex = 0
        End If
    End Sub
    'Remplit le comboBox par le nom de momatiKanouni
    Public Shared Sub remplirmomatil(ByRef cmb As ComboBox, ByRef type As String)
        moi.remplir_List_client()
        cmb.Items.Clear()
        cmb.Text = ""
        Dim cmd As New SqlClient.SqlCommand("select distinct Representant_legal from " & type & " where Representant_legal <> ''", Partager.con)
        open()
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                cmb.Items.Add(dr("Representant_legal"))
            End While
        End If
        close()
        If cmb.Items.Count > 0 Then
            cmb.SelectedIndex = 0
        End If
    End Sub
    'Remplit le comboBox par le nom et le prenom avec un id
    Public Shared Sub remplirEnemie(ByRef cmb As ComboBox)
        Partager.moi.remplir_List_enemi()
        cmb.Items.Clear()
        cmb.Text = ""
        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each en As Enemie In Partager.moi.List_enemi
            If en.Type = "ذاتي" Then
                str = en.Nom & "  " & en.Prenom
            ElseIf en.Type = "معنوي" Then
                str = en.Nom_ent
            End If
            htClient.Add(en.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            cmb.Items.Add(ImageType)
        Next
        cmb.DisplayMember = "value"
        cmb.ValueMember = "key"
        If cmb.Items.Count > 0 Then
            cmb.SelectedIndex = 0
        End If

    End Sub

    Public Shared Sub clientsReference(ByVal idRef As Integer, ByRef lst As ListView)
        Dim cmd As New SqlClient.SqlCommand("select C.* from Clients C inner join Reference" & _
        " on idClt = Id where Ref = " & idRef, Partager.con)
        Dim nomclient As String
        open()
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                If dr("Type_Client") = "ذاتي" Then
                    nomclient = dr("Nom") & " " & dr("Prenom")
                Else
                    nomclient = dr("Nom_entreprise")
                End If
                line = New ListViewItem(New String() {dr(0), nomclient})
                lst.Items.Add(line)
            End While
        End If
        close()
    End Sub

    'pour remplir le comboBox clients avec les noms et les references
    Public Shared Sub remplirClients(ByRef comb As ComboBox)
        '    moi.remplir_List_client()
        '    moi.remplir_Ref()


        '    Dim htClient As New Hashtable()
        '    Dim str As String = ""
        '    Dim ref As Integer = 0
        '    Dim comp As Integer
        '    Dim tousClt As String = "" ' Contient tous les noms des clients d'un Ref 

        '    For Each refe As Reference In moi.List_Reference
        '        tousClt = refe.Ref & " : "
        '        comp = 1
        '        For Each it As Integer In refe.idsClt

        '            For Each clt As Client In moi.List_client
        '                If it = clt.Id Then

        '                    If clt.Type = "ذاتي" Then
        '                        str = clt.Nom & "  " & clt.Prenom
        '                    ElseIf clt.Type = "معنوي" Then
        '                        str = clt.Nom_ent
        '                    End If

        '                    If comp = refe.idsClt.Count Then
        '                        tousClt += str
        '                    Else
        '                        tousClt += str + " - "
        '                    End If
        '                    Exit For
        '                End If
        '            Next
        '            comp += 1
        '        Next

        '        htClient.Add(refe.Ref, tousClt)
        '    Next

        '    For Each ImageType As DictionaryEntry In htClient
        '        comb.Items.Add(ImageType)
        '    Next
        '    comb.DisplayMember = "value"
        '    comb.ValueMember = "key"
        '    If comb.Items.Count > 0 Then
        '        comb.SelectedIndex = 0
        '    End If
    End Sub

    Public Shared Sub remplirDossier(ByRef cmb As ComboBox, Optional ByRef archive As Boolean = False)
        moi.remplir_List_info_fichier()
        cmb.Items.Clear()
        cmb.Text = ""
        If archiv Then
            cmb.Items.Add("جميع الملفات")
            archiv = False
        End If
        Dim htClient As New Hashtable()
        Dim str As String = ""

        For Each dr As Dossier In moi.List_info_fichier
            If dr.Archive = False And archive = False Then

                str = dr.Num & "  |  " & dr.Type_appel & "  |  " & dr.Tribunal
                htClient.Add(dr.id, CStr(str))
            ElseIf dr.Archive = True And archive = True Then
                str = dr.Num & "  |  " & dr.Type_appel & "  |  " & dr.Tribunal
                htClient.Add(dr.id, CStr(str))

            End If
        Next
        For Each ImageType As DictionaryEntry In htClient
            cmb.Items.Add(ImageType)
        Next
        cmb.DisplayMember = "value"
        cmb.ValueMember = "key"

    End Sub
    'Remplit les combos Dossier avec une contrainte du type de la decision
    Public Shared Sub remplirDossierTypeDes(ByVal typeDes As String, ByRef cmb As ComboBox)
        cmb.Items.Clear()
        Dim htClient As New Hashtable()
        Dim str As String = ""
        Dim flag As Boolean
        Try
            Partager.moi.remplir_List_info_fichier()
            For Each dr As Dossier In Partager.moi.List_info_fichier
                flag = False
                If dr.Ref = CInt(Partager.deClt.Key) Then
                    dr.remplir_List_decisions()
                    For Each dic As decisions In dr.List_decisions
                        If dic.Type = "قطعي" Then
                            flag = True
                            Exit For
                        End If
                    Next


                    If typeDes = "قطعي" And flag Then
                        str = dr.Num & "  |  " & dr.Type_appel & "  |  " & dr.Tribunal
                        htClient.Add(dr.id, CStr(str))
                    ElseIf typeDes <> "قطعي" And Not flag Then
                        str = dr.Num & "  |  " & dr.Type_appel & "  |  " & dr.Tribunal
                        htClient.Add(dr.id, CStr(str))
                    End If
                End If
            Next

            For Each ImageType As DictionaryEntry In htClient
                cmb.Items.Add(ImageType)
            Next
            cmb.DisplayMember = "value"
            cmb.ValueMember = "key"
            If cmb.Items.Count > 0 Then
                cmb.SelectedIndex = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Cette fonction s'assure que le nom de la pièce jointe n'existe pas avant
    ''' </summary>
    ''' <param name="nom">le nom du fichier.</param>
    ''' <remarks>remark</remarks>
    Public Shared Function PieceExiste(ByVal nom As String) As Boolean
        Dim flag As Boolean = False
        For Each piece As Jouinture In list_piece
            If piece.Nom = nom Then
                flag = True
                Exit For
            End If
        Next
        Return flag
    End Function

    Public Structure clt
        Public id As Integer
        Public nom As String
    End Structure
    Public Shared rendVsStruct As rendezVousStruct
    Public Structure rendezVousStruct
        Public dateAlarme As Date
        Public rendezVous As Rendez_vous
    End Structure

    'ayoub test
    Public Shared Sub remplir_CombAdvr(ByVal idclt As Integer, ByRef comb As ComboBox)
        Dim htClient As New Hashtable()
        Dim str As String = ""
        Dim cmd As New SqlClient.SqlCommand("select distinct refAdv.*,nom,prenom,Nom_entreprise,Type_Adversaire from refAdv,Adversaire" & _
            " where refAdv.idAdv=Adversaire.Id and idRef in(select idRef from refClt where idClt= " & idclt & ")", Partager.con)
        open()
        dr = cmd.ExecuteReader
        While dr.Read
            If dr("Type_Adversaire") = "ذاتي" Then
                str = dr("Nom") & "  " & dr("Prenom")
            ElseIf Partager.dr("Type_Adversaire") = "معنوي" Then
                str = dr("Nom_entreprise")
            End If
            htClient.Add(dr("idAdv"), str)
        End While
        dr.Close()
        close()
        comb.Items.Clear()
        comb.Text = ""
        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub

    Public Shared Sub remplir_CombClient(ByVal idadv As Integer, ByRef comb As ComboBox)
        Dim htClient As New Hashtable()
        Dim str As String = ""
        Dim cmd As New SqlClient.SqlCommand("select distinct refClt.*,nom,prenom,Nom_entreprise,Type_Client from refClt,Clients " & _
            " where refClt.idClt=Clients.Id and idRef in(select idRef from refAdv where idAdv = " & idadv & ")", Partager.con)
        open()
        dr = cmd.ExecuteReader
        While dr.Read
            If dr("Type_Client") = "ذاتي" Then
                str = dr("Nom") & "  " & dr("Prenom")
            ElseIf Partager.dr("Type_Client") = "معنوي" Then
                str = dr("Nom_entreprise")
            End If
            htClient.Add(dr("idClt"), str)
        End While
        dr.Close()
        close()
        comb.Items.Clear()
        comb.Text = ""
        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub

    Public Shared Sub remplir_dossier(ByVal idClt As Integer, ByVal idAdvr As Integer, ByRef comb As ComboBox, Optional ByVal arch As Boolean = False)

        moi.remplir_List_info_fichier()

        comb.Items.Clear()
        comb.Text = ""

        Dim htClient As New Hashtable()
        Dim str As String = ""

        Dim cmd As New SqlClient.SqlCommand("select idRef from refAdv where idAdv=" & idAdvr & _
      " and idRef in (select idRef from refClt where idClt =" & idClt & ")", Partager.con)
        open()
        dr = cmd.ExecuteReader
        While dr.Read
            For Each dor As Dossier In moi.List_info_fichier
                If dor.Archive = False And dor.Ref = dr("idRef") And arch = False Then

                    str = dor.Num & "  |  " & dor.Type_appel & "  |  " & dor.Tribunal
                    htClient.Add(dor.id, CStr(str))
                ElseIf dor.Archive = True And dor.Ref = dr("idRef") And arch = True Then
                    str = dor.Num & "  |  " & dor.Type_appel & "  |  " & dor.Tribunal
                    htClient.Add(dor.id, CStr(str))

                End If
            Next
        End While
        dr.Close()
        close()
        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub
    Public Shared Sub SauvegardeAutomatique()
        '--------la creation du dossier-------------------
        If (Not System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\BackUp Avocat")) Then
            System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\BackUp Avocat")
        End If

        Dim files As New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\BackUp Avocat")
        Dim Query As IEnumerable(Of String) = From f In files.GetFiles
                                              Select f.Name

    End Sub

    Public Shared maLstAdver As List(Of Partager.clt)
    'Public Shared Sub remplir_combAdv(ByVal idClt As Integer, ByRef comb As ComboBox)

    '    comb.Items.Clear()
    '    comb.Text = ""
    '    Dim htClient As New Hashtable()
    '    Dim str As String = ""
    '    Dim khab As New Partager.clt

    '    Dim cmd As New SqlClient.SqlCommand("select a.Id,a.Nom,a.Prenom,a.Nom_entreprise,Type_Adversaire,t.idRef from Adversaire a,(select idRef," & _
    '                                        " idAdv from refAdv where idRef in (select idRef from refClt where idClt = " & idClt & "))t where a.Id = t.idAdv", Partager.con)
    '    open()
    '    dr = cmd.ExecuteReader
    '    While dr.Read

    '        khab.id = Partager.dr("Id")
    '        If dr("Type_Adversaire") = "ذاتي" Then
    '            str = dr("Nom") & "  " & dr("Prenom")
    '            khab.nom = dr("Nom") & "  " & dr("Prenom")
    '        ElseIf Partager.dr("Type_Adversaire") = "معنوي" Then
    '            str = dr("Nom_entreprise")
    '            khab.nom = Partager.dr("Nom_entreprise")
    '        End If
    '        htClient.Add(dr("Id"), str)
    '        maLstAdver.Add(khab)

    '        htClient.Add(Partager.dr("Id"), str)
    '    End While

    '    dr.Close()
    '    close()

    '    For Each ImageType As DictionaryEntry In htClient
    '        comb.Items.Add(ImageType)
    '    Next
    '    comb.DisplayMember = "value"
    '    comb.ValueMember = "key"
    '    If comb.Items.Count > 0 Then
    '        comb.SelectedIndex = 0
    '    End If

    'End Sub


    ''' <summary>
    ''' Cette procèdure remplie un combo avec les adverssaires d'un client donnée,
    ''' le combo avoir en key le ref et en valeur le nom de l'adverssaire
    ''' il remplit aussi un arraylist par un objet de type Partager.clt avec en nom le ref
    ''' </summary>
    ''' <param name="idclt">L'id du client</param>
    ''' <param name="comb">combo Adverssaire</param>
    ''' <remarks>remark</remarks>
    Public Shared Sub remplir_CombAdvrT(ByVal idclt As Integer, ByRef comb As ComboBox)
        Dim htClient As New Hashtable()
        Dim str As String = ""
        Dim khab As New Partager.clt
        maLstAdver = New List(Of Partager.clt)


        Dim cmd As New SqlClient.SqlCommand("select refAdv.idRef,Id,nom,prenom,Nom_entreprise,Type_Adversaire from refAdv,Adversaire" & _
            " where refAdv.idAdv=Adversaire.Id and idRef in( select idRef from refClt where idClt= " & idclt & ")", Partager.con)
        open()
        dr = cmd.ExecuteReader
        While dr.Read
            khab.id = dr("Id")
            If dr("Type_Adversaire") = "ذاتي" Then
                str = dr("Nom") & "  " & dr("Prenom")
            ElseIf Partager.dr("Type_Adversaire") = "معنوي" Then
                str = dr("Nom_entreprise")
            End If
            khab.nom = dr("idRef")
            htClient.Add(dr("idRef"), str)
            maLstAdver.Add(khab)
        End While
        dr.Close()
        close()

        comb.Items.Clear()
        comb.Text = ""
        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub



    'Public Shared Sub MesAdversaires(ByVal id As Integer)

    '    maLstAdver = New List(Of Partager.clt)
    '    Dim clt As New SqlClient.SqlCommand("select Id,Nom,Prenom,Nom_entreprise,Type_Adversaire from Adversaire " & _
    '                                        "where Id in " & _
    '                                        "(select idAdv from refAdv " & _
    '                                        "where idRef in " & _
    '                                        "(select idRef from refClt " & _
    '                                        "where idClt = " & id & "))", Partager.con)
    '    Partager.open()
    '    Partager.dr = cmd.ExecuteReader
    '    While Partager.dr.Read
    '        Dim khab As New Partager.clt
    '        khab.id = Partager.dr("Id")

    '        If Partager.dr("Type_Adversaire") = "ذاتي" Then
    '            khab.nom = Partager.dr("Nom") & "  " & Partager.dr("Prenom")
    '        ElseIf Partager.dr("Type_Adversaire") = "معنوي" Then
    '            khab.nom = Partager.dr("Nom_entreprise")
    '        End If

    '        maLstAdver.Add(khab)
    '    End While
    '    Partager.close()
    '    Partager.dr.Close()
    'End Sub

    Public Shared Function partiesOntRef(ByVal idAdv As Integer, ByVal idClt As Integer) As Boolean
        Dim cmd As New SqlClient.SqlCommand("select * from refClt" & _
        " where(idClt = " & idClt & ")" & _
" and idRef in(" & _
" select idRef from refAdv" & _
" where idAdv = " & idAdv & ")", con)
        open()
        dr = cmd.ExecuteReader
        Dim aDesLigne As Boolean = dr.HasRows
        dr.Close()
        close()
        Return aDesLigne
    End Function


    ''' <summary>
    ''' Cette procédure remplie un le combo avec les noms des tribunals et leurs id 
    ''' </summary>
    ''' <remarks>remark</remarks>
    Public Shared Sub remplirTribunal(ByRef cmb As ComboBox)

        cmb.Items.Clear()
        cmb.Text = ""
        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each clt As Client In Partager.moi.List_client
            If clt.Type = "ذاتي" Then
                str = clt.Nom & "  " & clt.Prenom
            ElseIf clt.Type = "معنوي" Then
                str = clt.Nom_ent
            End If
            htClient.Add(clt.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            cmb.Items.Add(ImageType)
        Next
        cmb.DisplayMember = "value"
        cmb.ValueMember = "key"
        If cmb.Items.Count > 0 Then
            cmb.SelectedIndex = 0
        End If
    End Sub


    ''' <summary>
    ''' Cette procèdure remplie un combo avec les sources,
    ''' le combo avoir en key l'idSource et en valeur le nom de la source
    ''' </summary>
    ''' <remarks>remark</remarks>
    Public Shared Sub remplircmbSource(ByRef comb As ComboBox)
        Dim htClient As New Hashtable()

        For Each src In moi.lstSource
            htClient.Add(src.id, src.nom)
        Next
        comb.Items.Clear()
        comb.Text = ""
        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub
    'weather
    Public Shared Sub remplirCmbPlainte(ByRef cmb As ComboBox)
        moi.remplir_List_plainte()
        cmb.Text = ""

        Dim str As String = ""
        Dim dic As DictionaryEntry

        For Each plt As Plainte In moi.List_Plainte
            dic = New DictionaryEntry
            dic.Key = plt.Id
            dic.Value = plt.Num_Plainte & "/" & plt.Type.Split("-")(0) & "/" & plt.Annee
            cmb.Items.Add(dic)
        Next

        cmb.DisplayMember = "value"
        cmb.ValueMember = "key"
        If cmb.Items.Count > 0 Then
            cmb.SelectedIndex = 0
        End If
    End Sub

    Public Shared Function refParties(ByVal idClt As Integer, ByVal idAdv As Integer) As Integer
        Dim cmd As New SqlClient.SqlCommand("select refClt.idRef from refClt inner join refAdv " & _
                                            "on refAdv.idRef = refClt.idRef " & _
                                            "where idClt = " & idClt & _
                                            "and idAdv = " & idAdv, Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        Dim ref As Integer = -1
        If dr.HasRows Then
            While Partager.dr.Read
                ref = dr.GetInt32(0)
            End While
        End If
        Partager.dr.Close()
        Partager.close()
        Return ref
    End Function
End Class
